﻿using System.Linq;
using System.Web.Mvc;
using LogikBug.ModularMvc.Navigation;

namespace LogikBug.ModularMvc.Factories
{
	public abstract class MenuFactory : IMenuFactory
	{
		#region Properties

		public abstract MenuNode NodeType { get; }

		public IModuleProvider Provider
		{
			get
			{
				return ModuleProvider.Current;
			}
		}

		#endregion

		#region Methods

		public virtual MvcHtmlString Generate(ViewContext viewContext)
		{
			return Generate(viewContext, MenuType.Main);
		}

		public virtual MvcHtmlString Generate(ViewContext viewContext, MenuType menuType)
		{
			var menu = new Menu(NodeType);

			foreach (var module in Provider.OfType<IVisualModule>().Where(m => m.IsEnabled).OrderBy(m => m.MenuDisplayOrder))
			{
				var menuItems = module.GetMenu(viewContext, menuType);

				if (menuItems == null) { continue; }

				menu.MenuItems.AddRange(menuItems);
			}

			return menu.ToMvcHtmlString(menuType);
		}

		#endregion
	}
}
